package com.cn.indoors.testdemo;

import android.app.Application;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.cn.indoors.testdemo.utils.JsonUtils;
import com.cn.indoors.txlocationlib.GetTXLocationListener;
import com.cn.indoors.txlocationlib.TXLocationManager;
import com.cn.indoors.txlocationlib.TXPosition;

public class FirstFragment extends Fragment {
    TXLocationManager lc = TXLocationManager.getInstance();
    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        final Application app = this.getActivity().getApplication();
        super.onViewCreated(view, savedInstanceState);
        TXLocationManager.setGetTXLocationListener(new GetTXLocationListener() {
            @Override
            public void TXLocation(TXPosition txPosition) {
                Log.i("show1",String.valueOf(txPosition.getX()) + "," + String.valueOf(txPosition.getY()));
                Log.i("show2",JsonUtils.ObjectToJson(txPosition));
            }

            @Override
            public void TXError(String s) {
                Log.i("erroe",s);
            }
        });
        view.findViewById(R.id.button_init).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lc.init(app);
                Log.i("step1","初始化蓝牙信标搜索");
                try {
                    lc.initBeaconMap("mapbeacon.json");
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        view.findViewById(R.id.button_startScan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("step2","开始蓝牙搜索");
                lc.SetMapId("14");
                lc.SetLocationUrl("https://www.52mibee.com/locationservice/getLocation_v2/Trilateral");
                lc.SetLocationType(2);

                lc.SetUpelecURL("https://www.52mibee.com/merchshopmanage/service/setbeaconelectric");
                lc.SetUpelecFlag(true);
                lc.StartScan();


                Log.i("test","1");
            }
        });

        view.findViewById(R.id.button_stopScan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("step3","停止蓝牙搜索");
                lc.StopScan();
            }
        });
    }
}